import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, export_graphviz
# import pickle
from sklearn.externals import joblib
from sklearn.model_selection import GridSearchCV
from collections import Counter
from sklearn.metrics import accuracy_score
import numpy as np
y_scale = 1.6
y_column = 'up_945'
def buy_flow3(df, tezheng, dec):
    i = 0
    money = 10000
    err_count = 0
    all_count = 0
    sum_all = 0
    count_map = {}
    err_count_map = {}
    x_test = df[tezheng].values
    x = df[["symbol", 'date', '1d_up']].values
    y_test = (df[y_column] / y_scale).values
    for res in dec.predict(x_test):
        if str(res) in count_map:
            count_map[str(res)] = count_map[str(res)] + 1
        else:
            count_map[str(res)] = 1
        if res > 0:
            if x[i][2] > 9.9:
                continue
            all_count = all_count + 1
            print(res, "|||", y_test[i], x[i])
            if y_test[i] < 0:
                err_count = err_count + 1
                if str(res) in err_count_map:
                    err_count_map[str(res)].append(y_test[i])
                else:
                    err_count_map[str(res)] = [y_test[i]]
            #money = money + 10000 * (y_test[i] * y_scale/100)
            sum_all = sum_all + y_test[i] * y_scale
            money = money * (1 + y_test[i] * y_scale / 100)
        i = i + 1
    print(count_map)
    print("error", err_count, err_count_map)
    print("--交易了----", all_count, ":", money, "mean", sum_all/all_count)
def decision(df, df1):

    tezheng = [
        #'act_buy_xl_20', 'act_buy_xl_40', 'act_buy_xl_80', 'act_buy_xl_160',
        #'act_sell_xl_20', 'act_sell_xl_40', 'act_sell_xl_80', 'act_sell_xl_160',
        #'act_buy_xl_20_r', 'act_buy_xl_40_r', 'act_buy_xl_80_r', 'act_buy_xl_160_r',
        #'act_buy_l_20', 'act_buy_l_40', 'act_buy_l_80', 'act_buy_l_160',
        #'act_sell_l_20', 'act_sell_l_40', 'act_sell_l_80', 'act_sell_l_160',
        #'act_buy_l_20_r', 'act_buy_l_40_r',
        'act_buy_l_80_r',
        'act_buy_l_160_r',
        'act_buy_m_20', 'act_buy_m_40',
        'act_buy_m_80', 'act_buy_m_160',
        'act_sell_m_20',  'act_buy_m_20_r',
        'act_sell_m_40', 'act_buy_m_40_r',
        'act_sell_m_80', 'act_sell_m_160',
        'act_buy_m_80_r', 'act_buy_m_160_r',
        # 'dde_20', 'dde_40', 'dde_80', 'dde_160',
        'up_20m', 'up_40m', 'up_80m', 'up_160m',
        # '1d_up', '2d_up',
        # '3d_up', '5d_up',
        'turnover_rate', 'turnover_rate_1', 'turnover_rate_2', 'turnover_rate_3',
        #'turnover'
    ]


    print(df.columns)
    x_train = df[tezheng].values
    y_train = (df[y_column]/y_scale).values
    x_test = df1[tezheng].values
    y_test = (df1[y_column]/y_scale).values
    print("X_train_shape:", x_train.shape, " y_train_shape:", y_train.shape)
    print("X_test_shape:", x_test.shape, "  y_test_shape:", y_test.shape)

    print('Counter(data)\n', Counter(np.around(y_test)))
    # max_depth 5 或 6
    # 剪纸
    # 随机森林
    # dec = DecisionTreeClassifier(max_depth=6) #
    # dec.fit(x_train, np.round(y_train).astype('int'))
    # joblib.dump(dec, 'dec_01.pkl')
    dec = joblib.load('dec_01.pkl')
    # print("决策树准确率", dec.score(x_test, y_test.astype('int')))

    buy_flow3(df1, tezheng, dec)
    print("------------------")
    important = dict(zip(tezheng, dec.feature_importances_))
    export_graphviz(dec, feature_names=tezheng, out_file="./tree_v3_03_1v.dot")
    # dot -Tpdf tree.dot -o tree.pdf
    d_order = sorted(important.items(), key=lambda x: x[1], reverse=False)
    for i in d_order:
        print(i)
    print("end")


if __name__ == "__main__":

    # df = pd.read_csv("E:\\ts_data\\moneyflow\\allv2_SZZS_2019.csv")
    # df.dropna(inplace=True)
    # df.drop(df[df['act_buy_m_20_r'] == 0].index, inplace=True)
    df1 = pd.read_csv("E:\\ts_data\\moneyflow\\allv2_SZZS_2020.csv")
    df1.dropna(inplace=True)
    df1.drop(df1[df1['act_buy_m_20_r'] == 0].index, inplace=True)

    # df = df[(df.act_buy_m_160_r < 0.75) & (df.act_buy_m_160_r > 0.5)
    #         & (df.act_buy_l_160_r < 0.75) & (df.act_buy_l_160_r > 0.5)
    #         & (df.up_80m < 2) & (df.up_80m > -1)
    #         & (df.turnover_rate < 6)]
    # df1 = df1[(df1.act_buy_m_160_r < 0.75) & (df1.act_buy_m_160_r > 0.5)
    #     & (df1.act_buy_l_160_r < 0.75) & (df1.act_buy_l_160_r > 0.5)
    #     ]
    split = int(df1.shape[0] * 0.8)
    decision(df1[:split], df1[split:])
    #decision(df, df1)

    # df = pd.read_csv("E:\\ts_data\\moneyflow\\all.csv")
    # std =StandardScaler()
    # data = std.fit_transform(df[['dde_l', 'dde_10', 'dde_20', 'dde_40', 'dde_80']].values)
    # print(data)


    ### 结论  up_160m 在 0 到 2 之间
    ### 结论  up_80m 在 -1 到 2 之间
    ### 结论  up_40m 在 -0.5 到 0.5 之间
    ### 结论  up_20m 在 -0.5 到 0.5 之间

    ### 换手率 大于百分之四十二不要了
    ###  act_buy_m_xxx_r 0.5~0.7 平均是正的 , 0.6 是最优
    ###  act_buy_l_160_r 0.5 ~ 0.7 附近    需要看更长时间， 2小时，日线， 三日， 五日
    # act_buy_m_160 15 到 20.5
    ## turnover_rate 0 ~ 3   越大 越不安全
    ### 换手率越大 应该 大单净买越重要